package com.asset.dispoal.business.repayment.service.strategy;

import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementInfo;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.repayment.domain.db.entity.BsCaseRepayment;
import com.asset.dispoal.business.repayment.domain.db.mapper.CaseRepaymentMapper;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 没有立案选择公式计算
 * @author: yf
 * @date: 2022-09-27 11:01:45
 */
@Component("waitUpCaseState")
public class WaitUpCase implements IRepaymentStrategy {
    @Autowired
    private AgreementMapper agreementMapper;
    @Autowired
    private CaseRepaymentMapper caseRepaymentMapper;

    @Override
    public RepaymentInfoResponse getRepaymentInfo(BsCase bsCase) {
        AgreementCaseSateMenu caseSateMenu = AgreementCaseSateMenu.getByLabel(bsCase.getCaseState());
        //还款信息是否存在
        BsCaseRepayment caseRepayment = caseRepaymentMapper.queryByCaseId(bsCase.getId(), bsCase.getCaseState());
        if (null != caseRepayment) {
            //已还本金 利息 罚息
            BigDecimal repaymentPrincipal = caseRepayment.getRepaymentPrincipal() == null ? BigDecimal.ZERO : caseRepayment.getRepaymentPrincipal();
            BigDecimal repaymentInterest = caseRepayment.getRepaymentInterest() == null ? BigDecimal.ZERO : caseRepayment.getRepaymentInterest();
            BigDecimal repaymentPenalty = caseRepayment.getRepaymentPenalty() == null ? BigDecimal.ZERO : caseRepayment.getRepaymentPenalty();
            //待还本金 则判决还款中依次扣减以前所还金额
            BigDecimal toDoReturnPrincipalAmount = bsCase.getPrincipal().subtract(repaymentPrincipal);
            BigDecimal toDoInterestAmount = bsCase.getInterest().subtract(repaymentInterest);
            BigDecimal toDoLitigationFines = bsCase.getPenaltyInterest().subtract(repaymentPenalty);

            return RepaymentInfoResponse.builder()
                    .repaymentId(caseRepayment.getId())
                    .caseId(bsCase.getId())
                    .caseState(bsCase.getCaseState())
                    .caseStateName(caseSateMenu.getValue())
                    .lawsuitPrincipalAmount(bsCase.getPrincipal().toPlainString())
                    .lawsuitInterestAmount(bsCase.getInterest().toPlainString())
                    .litigationFines(bsCase.getPenaltyInterest().toPlainString())
                    .toDoReturnPrincipalAmount(toDoReturnPrincipalAmount.toPlainString())
                    .toDoInterestAmount(toDoInterestAmount.toPlainString())
                    .toDoLitigationFines(toDoLitigationFines.toPlainString())
                    .repaymentPrincipal(repaymentPrincipal.toPlainString())
                    .repaymentInterest(repaymentInterest.toPlainString())
                    .repaymentPenalty(repaymentPenalty.toPlainString())
                    .litigationAmount(bsCase.getCaseAcceptAmount().toPlainString())
                    .preservationAmount(bsCase.getJudgmentPreservationAmount().toPlainString())
                    .build();
        }

        ///合同集合，未处理逾期时间与总额
        List<AgreementInfo> agreementInfos = agreementMapper.queryAgreementInfoByCaseId(bsCase.getId());
        //应还本金
        String principalRepayment = agreementInfos.stream()
                .map(AgreementInfo::getLawsuitPrincipalAmount)
                .map(BigDecimal::new)
                .reduce(BigDecimal.ZERO, BigDecimal::add).toString();
        //应还利息
        String interestRepayment = agreementInfos.stream()
                .map(AgreementInfo::getLawsuitInterestAmount)
                .map(BigDecimal::new)
                .reduce(BigDecimal.ZERO, BigDecimal::add).toString();

        return RepaymentInfoResponse.builder()
                .caseId(bsCase.getId())
                .caseState(bsCase.getCaseState())
                .caseStateName(caseSateMenu.getValue())
                .lawsuitPrincipalAmount(principalRepayment)
                .lawsuitInterestAmount(interestRepayment)
                .toDoReturnPrincipalAmount(principalRepayment)
                .toDoInterestAmount(interestRepayment)
                .repaymentPrincipal(BigDecimal.ZERO.toPlainString())
                .repaymentInterest(BigDecimal.ZERO.toPlainString())
                .build();
    }

}
